package son.vpoint.dinokids.until;

import java.util.LinkedList;

import org.anddev.andengine.util.MathUtils;

public class MathFactory {
	public static final int DIFF_EASY = 0;
	public static final int DIFF_NORMAL = 1;
	public static final int DIFF_HARD = 2;
	public static final int MAX_QUESTION_NUMBER = 10;
	public static final int OPERATOR_ADD = 0;
	public static final int OPERATOR_SUB = 1;
	public static final int OPERATOR_MULTI = 2;
	public static final int OPERATOR_DIVIS = 3;
	public static final int OPERATOR_MIX = 4;
	public static final int PARAM_ANSWER = -1;
	private static final String QUESTION_NO_TEMPLATE = "Question %d / %d";
	private int mCurrentQuestionNo = 0;
	private int mDifficultLevel = 0;
	private int mRange = 648;
	private int mOperator = 0;
	private int param1 = -1;
	private int param2 = 0;
	private int param3 = 0;
	private int mResult = 0;

	public static LinkedList<Integer> conventionsCorrect(int paramInt) {
		LinkedList localLinkedList = new LinkedList();
		for (int i = 1; i < 10; i++) {

			if ((paramInt % i == 0) && (paramInt / i >= 1)
					&& (paramInt / i <= 9))
				localLinkedList.add(i);
		}
		return localLinkedList;
	}

	private void nextAddQuestion() {
		switch (mDifficultLevel) {

		case 0:

			if (mCurrentQuestionNo != 10) {
				mCurrentQuestionNo = (1 + mCurrentQuestionNo);
				mResult = MathUtils.random(2, Math.min(mRange, 18));
				do {
					param1 = MathUtils.random(1, 9);
					param2 = (mResult - param1);
				} while ((param2 < 1) || (param2 > 9));
				if (MathUtils.random(0, 1) == 0)
					param1 = -1;
				else
					param2 = -1;
			} else {
			mDifficultLevel = 1;
				mCurrentQuestionNo = 0;
				onLevelUp();
			}
			break;
		case 1:
			if (mCurrentQuestionNo != 10) {
				mCurrentQuestionNo = (1 + mCurrentQuestionNo);
				mResult = MathUtils.random(3, Math.min(mRange, 17));
				param1 = -1;
				param2 = -1;
			} else {
				mDifficultLevel = 2;
				mCurrentQuestionNo = 0;
				onLevelUp();
			}
			break;
		case 2:
			if (mCurrentQuestionNo == 10)
				break;
			mCurrentQuestionNo = (1 + mCurrentQuestionNo);
			mResult = MathUtils.random(4, Math.min(mRange, 26));
			int i;
			do
				i = MathUtils.random(1, 9);
			while ((mResult - i > 17) || (mResult - i < 3));
			switch (MathUtils.random(1, 3)) {

			case 1:
				param1 = i;
				param2 = -1;
				param3 = -1;
				break;
			case 2:
				param1 = -1;
				param2 = i;
				param3 = -1;
				break;
			case 3:
				param1 = -1;
				param2 = -1;
				param3 = i;
				break;
			default:
				break;
			}
		default:
			onGameWin();

		}

	}

	private void nextSubQuestion() {
		switch (mDifficultLevel) {
		default:
			onGameWin();
		case 0:
			if (mCurrentQuestionNo != 10) {
				mCurrentQuestionNo = (1 + mCurrentQuestionNo);
				mResult = MathUtils.random(0, Math.min(mRange, 8));
				do {
					param1 = MathUtils.random(1, 9);
					param2 = (param1 - mResult);
				} while ((param2 < 1) || (param2 > 9));
				if (MathUtils.random(0, 1) == 0)
					param1 = -1;
				else
					param2 = -1;
			} else {
				mDifficultLevel = 1;
				mCurrentQuestionNo = 0;
				onLevelUp();
			}
			break;
		case 1:
			if (mCurrentQuestionNo != 10) {
				mCurrentQuestionNo = (1 + mCurrentQuestionNo);
				mResult = MathUtils.random(1, Math.min(mRange, 8));
				param1 = -1;
				param2 = -1;
			} else {
				mDifficultLevel = 2;
				mCurrentQuestionNo = 0;
				onLevelUp();
			}
			break;
		case 2:
			if (mCurrentQuestionNo == 10)
				break;
			mCurrentQuestionNo = (1 + mCurrentQuestionNo);
			switch (MathUtils.random(1, 3)) {

			case 1:
				int k = MathUtils.random(3, 9);
				mResult = MathUtils.random(0, Math.min(mRange, 9 - k));
				param1 = (k + mResult);
				param2 = -1;
				param3 = -1;
				break;
			case 2:
				int j = MathUtils.random(1, 8);
				param2 = MathUtils.random(1, Math.min(mRange, j));
				mResult = (j - param2);
				param1 = -1;
				param3 = -1;
				break;
			case 3:
				int i = MathUtils.random(1, 8);
				param3 = MathUtils.random(1, Math.min(mRange, i));
				mResult = (i - param3);
				param1 = -1;
				param2 = -1;
				break;
			default:
				break;
			}

		}

	}

	private void nextMulQuestion()
	 {
	 switch (mDifficultLevel)
	 {

	 case 0:
		 if (mCurrentQuestionNo != 10)
		 {
			mCurrentQuestionNo = (1 + mCurrentQuestionNo);
		 LinkedList localLinkedList5;
		 do
		 {
		 mResult = MathUtils.random(1, Math.min(mRange, 81));
		 localLinkedList5 = conventionsCorrect(mResult);
		 }
		 while (localLinkedList5.size() == 0);
		 param1 = ((Integer) localLinkedList5.get(MathUtils.random(0, -1 +
		 localLinkedList5.size()))).intValue();
		 param2 = (mResult / param1);
		 if (MathUtils.random(0, 1) == 0)
		 param1 = -1;
		 else
		 param2 = -1;
		 }
		 else
		 {
		 mDifficultLevel = 1;
		 mCurrentQuestionNo = 0;
		 onLevelUp();
		 }
		 break;
	case 1:
		 break;
	case 2:
		break;
	 default:
	 onGameWin();
	 break;
	 }
	 }

	private void nextMixQuestion() {
		
		
	}

	private void nextDivisQuestion() {
		
		
	}
	private void onLevelUp() {

	}

	private void onGameWin() {

	}

	public String getQuestionNoString() {
		Object[] arrayOfObject = new Object[2];
		arrayOfObject[0] = Integer.valueOf(this.mCurrentQuestionNo);
		arrayOfObject[1] = Integer.valueOf(10);
		return String.format("Question %d / %d", arrayOfObject);
	}
	public int getDifficultLevel(){
		return mDifficultLevel;
	}
	public int getParam1()
	  {
	    return param1;
	  }

	  public int getParam2()
	  {
	    return param2;
	  }

	  public int getParam3()
	  {
	    return param3;
	  }
	  public int getResult()
	  {
	    return mResult;
	  }
	public void nextQuestion(){
		switch (mOperator) {
		case 0:
			nextAddQuestion();
			break;
		case 1:
			nextSubQuestion();
			break;
		case 2:
			nextMulQuestion();
			break;
		case 3:
			nextDivisQuestion();
			break;
		case 4:
			nextMixQuestion();
			break;

		default:
			break;
		}
	}


}
